﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 水费管理系统
{
    public partial class 收费录入 : Form
    {
        private bool is_new = false;
        public 收费录入()
        {
            InitializeComponent();
        }

        private void 收费录入_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“dataSet1.抄表”中。您可以根据需要移动或移除它。
            //this.抄表TableAdapter.Fill(this.dataSet1.抄表);
            // TODO: 这行代码将数据加载到表“dataSet1.基本信息”中。您可以根据需要移动或移除它。
            //this.基本信息TableAdapter.Fill(this.dataSet1.基本信息);
            // TODO: 这行代码将数据加载到表“dataSet1.用水单位信息”中。您可以根据需要移动或移除它。
            //this.用水单位信息TableAdapter.Fill(this.dataSet1.用水单位信息);
            // TODO: 这行代码将数据加载到表“dataSet1.收费”中。您可以根据需要移动或移除它。
            //this.收费TableAdapter.Fill(this.dataSet1.收费);
            // TODO: 这行代码将数据加载到表“dataSet1.设置年月”中。您可以根据需要移动或移除它。
            //this.设置年月TableAdapter.Fill(this.dataSet1.设置年月);
            // TODO: 这行代码将数据加载到表“dataSet1.抄表”中。您可以根据需要移动或移除它。
            //this.抄表TableAdapter.Fill(this.dataSet1.抄表);
            //this.设置年月TableAdapter.FillByYx(this.dataSet1.设置年月);
            //comboBox2.Text = this.dataSet1.设置年月[0].年度;
            //comboBox3.Text = this.dataSet1.设置年月[0].月份;]
            this.收费BindingSource.AddNew();
            bindingNavigatorAddNewItem_Click(sender, e);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        public static string DoubleToDaxie(decimal num)
        {
            string str1 = "零壹贰叁肆伍陆柒捌玖";            //0-9所对应的汉字
            string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
            string str3 = "";    //从原num值中取出的值
            string str4 = "";    //数字的字符串形式
            string str5 = "";  //人民币大写金额形式
            int i;    //循环变量
            int j;    //num的值乘以100的字符串长度
            string ch1 = "";    //数字的汉语读法
            string ch2 = "";    //数字位的汉字读法
            int nzero = 0;  //用来计算连续的零值是几个
            int temp;            //从原num值中取出的值

            num = Math.Round(Math.Abs(num), 2);    //将num取绝对值并四舍五入取2位小数
            str4 = ((long)(num * 100)).ToString();        //将num乘100并转换成字符串形式
            j = str4.Length;      //找出最高位
            if (j > 15) { return "溢出"; }
            str2 = str2.Substring(15 - j);   //取出对应位数的str2的值。如：200.55,j为5所以tr2=佰拾元角分

            //循环取出每一位需要转换的值
            for (i = 0; i < j; i++)
            {
                str3 = str4.Substring(i, 1);          //取出需转换的某一位的值
                temp = Convert.ToInt32(str3);      //转换为数字
                if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
                {
                    //当所取位数不为元、万、亿、万亿上的数字时
                    if (str3 == "0")
                    {
                        ch1 = "";
                        ch2 = "";
                        nzero = nzero + 1;
                    }
                    else
                    {
                        if (str3 != "0" && nzero != 0)
                        {
                            ch1 = "零" + str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                        else
                        {
                            ch1 = str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                    }
                }
                else
                {
                    //该位是万亿，亿，万，元位等关键位
                    if (str3 != "0" && nzero != 0)
                    {
                        ch1 = "零" + str1.Substring(temp * 1, 1);
                        ch2 = str2.Substring(i, 1);
                        nzero = 0;
                    }
                    else
                    {
                        if (str3 != "0" && nzero == 0)
                        {
                            ch1 = str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                        else
                        {
                            if (str3 == "0" && nzero >= 3)
                            {
                                ch1 = "";
                                ch2 = "";
                                nzero = nzero + 1;
                            }
                            else
                            {
                                if (j >= 11)
                                {
                                    ch1 = "";
                                    nzero = nzero + 1;
                                }
                                else
                                {
                                    ch1 = "";
                                    ch2 = str2.Substring(i, 1);
                                    nzero = nzero + 1;
                                }
                            }
                        }
                    }
                }
                if (i == (j - 11) || i == (j - 3))
                {
                    //如果该位是亿位或元位，则必须写上
                    ch2 = str2.Substring(i, 1);
                }
                str5 = str5 + ch1 + ch2;

                if (i == j - 1 && str3 == "0")
                {
                    //最后一位（分）为0时，加上“整”
                    str5 = str5 + '整';
                }
            }
            if (num == 0)
            {
                str5 = "零元整";
            }
            return str5;
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            is_new = true;

            this.dataSet1.设置年月.Clear();
            this.设置年月TableAdapter.FillByYx(this.dataSet1.设置年月);
            comb_年度.Text = this.dataSet1.设置年月[0].年度;
            comb_月份.Text = this.dataSet1.设置年月[0].月份;
            dtp_本次交费截止日期.Text = this.dataSet1.设置年月[0].滞纳金计算时间.ToString();

            this.dataSet1.用水单位信息.Clear();
            this.用水单位信息TableAdapter.Fill(this.dataSet1.用水单位信息);
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void 保存SToolStripButton_Click(object sender, EventArgs e)
        {
            if (is_new)
            {
                try
                {
                    this.dataSet1.基本信息[0].上月结余 = Convert.ToDouble(tb_本月结余.Text);
                    for (int i = 0; i < this.dataSet1.抄表.Count; i++)
                    {
                        this.dataSet1.抄表[i].已记录 = Convert.ToInt32(iDTextBox .Text);
                    }
                    this.Validate();
                    this.收费BindingSource.EndEdit();
                    this.收费TableAdapter.Update(this.dataSet1.收费);
                    this.基本信息TableAdapter.Update(this.dataSet1.基本信息);
                    this.抄表TableAdapter.Update(this.dataSet1.抄表);
                    //DialogResult dr = MessageBox.Show("保存成功，是否继续？", "提示", MessageBoxButtons.YesNo);
                    //if (dr == DialogResult.Yes)
                    //    this.抄表BindingSource.AddNew();
                    //is_new = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("非新增数据,无法保存!");
            }
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            try
            {
                if (!is_new) return;
                this.dataSet1.基本信息.Clear();
                this.基本信息TableAdapter.FillBy户号(this.dataSet1.基本信息, tb_户号.Text);
                if (this.dataSet1.基本信息.Count < 1)
                {
                    tb_户号.Text = "";
                    tb_户号.Focus();
                    throw(new System.Exception("无此户!"));
                }
                
                if (this.dataSet1.基本信息[0].Is上月结余Null()) tb_上月结转.Text = "0";
                else tb_上月结转.Text = Convert.ToString(this.dataSet1.基本信息[0].上月结余);
                tb_户名.Text = this.dataSet1.基本信息[0].户名;
                comb_用户单位.Text = this.dataSet1.基本信息[0].用户单位;
            


                comb_比例.Text = "0";


                this.dataSet1.抄表.Clear();
                this.抄表TableAdapter.FillBy户号(this.dataSet1.抄表, tb_户号.Text.Trim());
                if (this.dataSet1.抄表.Count < 1)
                {
                    tb_户号.Text = "";
                    tb_户号.Focus();
                    throw(new System.Exception("此户未抄表!"));
                }

                double 应交金额=0,滞纳金=0;
                int 过期天数=0;
                for (int i = 0; i < this.dataSet1.抄表.Count; i++)
                {
                    应交金额 += this.dataSet1.抄表[i].应交金额;
                    DateTime 滞纳金计算日期 = this.dataSet1.抄表[i].滞纳金日期 ;
                    过期天数 = Convert.ToInt32((dtp_交费日期.Value.Date - 滞纳金计算日期.Date).TotalDays-30);
                    if (过期天数 < 0) 过期天数 = 0;
                    this.dataSet1.抄表[i].过期天数 = 过期天数;
                    this.dataSet1.抄表[i].滞纳金 = this.dataSet1.抄表[i].应交金额 * 过期天数 * Convert.ToDouble(comb_比例.Text) / 1000;
                    滞纳金+=this.dataSet1.抄表[i].滞纳金 ;
                    this.dataSet1.抄表[i].总金额 = this.dataSet1.抄表[i].应交金额 + this.dataSet1.抄表[i].滞纳金;
                }

                滞纳金 = Math.Round(滞纳金, 2);
                tb_应交金额.Text = Convert.ToString(应交金额);
                tb_过期天数.Text = Convert.ToString(过期天数);
                tb_滞纳金.Text = Convert.ToString(滞纳金);
                tb_总金额.Text = Convert.ToString(应交金额+滞纳金);               

                tb_实交金额.Text = Convert.ToString (Math.Round(Convert.ToDouble(tb_总金额.Text) - Convert.ToDouble(tb_上月结转.Text),2));
                if (Convert.ToDouble(tb_实交金额.Text ) < 0) tb_实交金额.Text  = "0";
                tb_本月结余.Text = Convert.ToString(Math.Round(Convert.ToDouble(tb_上月结转.Text) + Convert.ToDouble(tb_实交金额.Text) - Convert.ToDouble(tb_总金额.Text), 2));

                tb_实交大写.Text =DoubleToDaxie(Convert.ToDecimal(tb_实交金额.Text));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void comboBox4_Leave(object sender, EventArgs e)
        {
            if (!is_new) return;
            if(tb_户号.Text=="")this.抄表TableAdapter.FillBy用户单位(this.dataSet1.抄表,comb_用户单位.Text);
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (!is_new) return;
            if (tb_户号.Text == "") this.抄表TableAdapter.FillBy户名(this.dataSet1.抄表, tb_户名.Text);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void tb_实交金额_TextChanged(object sender, EventArgs e)
        {
        }

        private void tb_实交金额_Leave(object sender, EventArgs e)
        {
            if (!is_new) return;
            try
            {
                if (tb_实交金额.Text.Trim() == "") tb_实交金额.Text  = "0";
                tb_本月结余.Text = Convert.ToString(Math.Round(Convert.ToDouble(tb_上月结转.Text) + Convert.ToDouble(tb_实交金额.Text) - Convert.ToDouble(tb_总金额.Text), 2));
                this.dataSet1.基本信息[0].上月结余 = Convert.ToDouble(tb_本月结余.Text);
                tb_实交大写.Text = DoubleToDaxie(Convert.ToDecimal(tb_实交金额.Text));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 打印PToolStripButton_Click(object sender, EventArgs e)
        {
            if (is_new)
            {
                MessageBox.Show("请先保存数据后,再打印!");
                return;
            }
            打印 dysj = new 打印();
            dysj.reportViewer1.LocalReport.ReportEmbeddedResource = "水费管理系统.报表.收据.rdlc";

            dysj.ShowDialog();
            dysj.Dispose();
        }

        private void iDTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!is_new) 
            {
                this.dataSet1.抄表.Clear();
                this.抄表TableAdapter.FillByID(this.dataSet1.抄表, Convert.ToInt32(iDTextBox.Text));
            }

        }

    }
}